﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Peekaboo2008.dao;
using Peekaboo2008.model;
using Peekaboo2008.common;
using System.Data.SqlClient;

namespace Peekaboo2008.view
{
    public partial class FormCongNoNhaCungCap : Form
    {
        String MaLoaiCongNo;
        int flag = 0;
        public FormCongNoNhaCungCap()
        {
            InitializeComponent();
            initLoaiCongNo();
            FormCongNoNCCLoad();            

        }
        private void initLoaiCongNo()
        {
            DataTable db = new DataTable();
            db = CongNoDAO.LoaiCongNo_GetAll();
            cbbLoaiCongNo.DataSource = db;
            flag = 1;
            cbbLoaiCongNo.ValueMember = "ID";
            cbbLoaiCongNo.DisplayMember = "Ten";
            cbbLoaiCongNo.SelectedIndex = 0;
            MaLoaiCongNo = cbbLoaiCongNo.SelectedValue.ToString();
        }
        public void FormCongNoNCCLoad()
        {
            DataTable dt = CongNoDAO.CongNo_NhaCungCap_GetAll(MaLoaiCongNo);
            dgvCongNoNCC.DataSource = dt;
        
        }

        private void dgvCongNoNCC_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            
            FormCongNoNhaCungCap formCongNoNCC = this;
            CongNoModel model = new CongNoModel();
            model.Id = dgvCongNoNCC.CurrentRow.Cells["clmID"].Value.ToString();
            model.Ma_loai_cong_no = dgvCongNoNCC.CurrentRow.Cells["clmMaLoaiCongNo"].Value.ToString();
            model.Ten = dgvCongNoNCC.CurrentRow.Cells["clmTen"].Value.ToString();
            model.Ma_khach_hang = dgvCongNoNCC.CurrentRow.Cells["clmMaKH"].Value.ToString();
            model.TenKH = dgvCongNoNCC.CurrentRow.Cells["clmTenKH"].Value.ToString();
            model.Ma_ngoai_te = dgvCongNoNCC.CurrentRow.Cells["clmMaNgoaiTe"].Value.ToString();
            model.Thanh_toan = dgvCongNoNCC.CurrentRow.Cells["clmThanhToan"].Value.ToString();
            model.Ty_gia = dgvCongNoNCC.CurrentRow.Cells["clmTyGia"].Value.ToString();
            model.Thanh_tien = dgvCongNoNCC.CurrentRow.Cells["clmThanhTien"].Value.ToString();
            model.Ghi_chu = dgvCongNoNCC.CurrentRow.Cells["clmGhiChu"].Value.ToString();
            model.NgayCapNhatCuoi = dgvCongNoNCC.CurrentRow.Cells["clmNgayCapNhatCuoi"].Value.ToString();

            FormChiTietCongNoNhaCungCap frmChiTietCongNoNCC = new FormChiTietCongNoNhaCungCap(model, ref formCongNoNCC);
            frmChiTietCongNoNCC.ShowDialog();
        }

        private void cbbLoaiCongNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flag == 0)
            {

            }
            else
            {
                MaLoaiCongNo = cbbLoaiCongNo.SelectedValue.ToString();
                FormCongNoNCCLoad();
            }
        }

        private void tsbThem_Click(object sender, EventArgs e)
        {
            FormCongNoNhaCungCap formCongNoNCC = this;
            FormChiTietCongNoNhaCungCap formChiTietCongNoNCC = new FormChiTietCongNoNhaCungCap(ref formCongNoNCC);
            formChiTietCongNoNCC.ShowDialog();
        }

        private void tsbThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void FormCongNoNhaCungCap_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                tsbThem.PerformClick();
            }
            else if (e.KeyCode == Keys.F4)
            {
                tsbThoat.PerformClick();
            }
        }

        private void FormCongNoNhaCungCap_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormCongNoNhaCungCap_KeyDown);
        }

    }
}
